Enter 1 if you are hungry; 0 if you are not ? 10 Buy Cookies Keep Shopping
The true branch was not skipped because the answer to the question
IF HUNGER > 0 THEN
was true.
The user entered something other than what the program
asked for.
The user entered 10 for
HUNGER
,
so the relational expression
HUNGER > 0
was true, so the true branch
was executed.
Programs with decisions (or loops) in them are controlled by relational expressions. Relational expressions always give TRUE or FALSE, even for undexpedted data. The user did not follow the instructions, but the relational expression went ahead and used what was entered to get TRUE. Then the true branch was executed. This might not be what the user wanted; perhaps the user thought 10 should mean "thirsty." Computer programs have a hard time with unexpected data, like the 10.
The IF-THEN-END
type of decision
has only one branch, which
is either skipped or not skipped.
If there is a block of statements you want to skip sometimes and
execute other times you put it in an
IF-THEN-END
stucture.
Now you just have to be sure to ask the right question in the IF
.
Say that you are at the Mall again, and have found a nice $44.95 sweater, but might not have enough money to buy it. (You spent too much on cookies.) Here is a program that decides if you can buy the sweater:
PRINT "How much money do you have" INPUT CASH ' IF ________________ THEN PRINT "Buy the Sweater" ' true branch END IF ' PRINT "done" END
What relational expression should be in the blank? (Assume that there is no sales tax.)